package com.navi168.fwq21.hyzl;

import java.util.LinkedList;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import com.amap.mapapi.core.GeoPoint;
import com.amap.mapapi.map.MapView;
import com.amap.mapapi.map.MyLocationOverlay;
import com.navi168.fwq21.R;
import com.navi168.fwq21.util.SendPosition;

public class MyLocationOverlayProxy extends MyLocationOverlay {

	private Location mLocation;
	private final LinkedList<Runnable> mRunOnFirstFix = new LinkedList<Runnable>();

	private MapView oMapView;
	private View s_e_popUp;
	private LayoutInflater s_e_inflater;
	private Context s_e_context;

	public MyLocationOverlayProxy(Context arg0, MapView arg1) {

		super(arg0, arg1);
		this.oMapView = arg1;
		this.s_e_context = arg0;
	}

	@Override
	public boolean runOnFirstFix(final Runnable runnable) {
		if (mLocation != null) {
			new Thread(runnable).start();
			return true;
		} else {
			mRunOnFirstFix.addLast(runnable);
			return false;
		}
	}

	@Override
	public void onLocationChanged(Location location) {
		mLocation = location;

		for (final Runnable runnable : mRunOnFirstFix) {
			new Thread(runnable).start();
		}

		/*
		 * oMapView.getController().animateTo( new GeoPoint((int)
		 * (location.getLatitude() * 1e6), (int) (location.getLongitude() *
		 * 1e6)));
		 */
		Log.i("mylog",
				mLocation.getLatitude() + "---" + mLocation.getLongitude());
		mRunOnFirstFix.clear();

		super.onLocationChanged(location);
	}

	@Override
	protected boolean dispatchTap() {
		Log.i("mylog", "in the click");
		s_e_inflater = (LayoutInflater) s_e_context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		s_e_popUp = s_e_inflater.inflate(R.layout.popup, null);
		TextView name = (TextView) s_e_popUp.findViewById(R.id.PoiName);
		TextView addr = (TextView) s_e_popUp.findViewById(R.id.PoiAddress);
		ImageButton xfButton = (ImageButton) s_e_popUp
				.findViewById(R.id.xf_button);
		ImageButton nearButton = (ImageButton) s_e_popUp
				.findViewById(R.id.button1);

		name.setText("当前位置");
		addr.setText("");
		MapView.LayoutParams lp;
		lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
				MapView.LayoutParams.WRAP_CONTENT, new GeoPoint(
						(int) (mLocation.getLatitude() * 1e6),
						(int) (mLocation.getLongitude() * 1e6)), 0, 0,
				MapView.LayoutParams.BOTTOM_CENTER);
		oMapView.addView(s_e_popUp, lp);
		s_e_popUp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Log.i("mylog", "in click");
				oMapView.removeView(s_e_popUp);

			}
		});

		xfButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(s_e_context, SendPosition.class);
				s_e_context.startActivity(intent);

			}
		});
		nearButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				Bundle bundle = new Bundle();
				bundle.putLong("lat",
						new Double(mLocation.getLatitude()).longValue());
				bundle.putLong("long",
						new Double(mLocation.getLongitude()).longValue());
				bundle.putString("class_name", s_e_context.getClass().toString());
				Intent it = new Intent();
				it.putExtras(bundle);
				it.setClass(s_e_context, NearPoi.class);
				s_e_context.startActivity(it);

			}
		});

		return super.dispatchTap();
	}

}
